package generateFile;

import entity.Book;
import service.BookService;
import service.impl.BookServiceImpl;

import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

public class BooksFile {

    public static void generateBookList(){
        BookService bookService=new BookServiceImpl();
        List<Book> books=bookService.queryAllBooks();
        FileWriter out=null;
        try {
            out=new FileWriter("D:\\Documents\\Desktop\\材料\\IDEA_workspace\\webLibrarySystem\\fileList\\书籍名单.txt");
            out.write("书籍ID\t\t\t");
            out.write("书籍名\t\t\t");
            out.write("作者\t\t\t");
            out.write("价格\t\t\t");
            out.write("出版社\n");
            for(Book book:books){
                out.write(book.getBookId()+"\t\t");
                out.write(book.getBookName()+"\t\t\t");
                out.write(book.getAuthor()+"\t\t\t");
                out.write(book.getPrice()+"\t\t\t");
                out.write(book.getPublisher()+"\n");
            }
            out.flush();
            System.out.println("书籍名单生成！");
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}
